package com.yeban.javase.day20.reflect.singleton;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;

/**
 * 枚举   -   多例
 */
public enum MyEnum {

    INSTANCE;

    public static MyEnum getInstance() {
        return INSTANCE;
    }
}

class Test4{
    public static void main(String[] args) throws NoSuchMethodException, InvocationTargetException, InstantiationException, IllegalAccessException {
        MyEnum myEnum1 = MyEnum.getInstance();
        MyEnum myEnum2 = MyEnum.getInstance();
        System.out.println(myEnum1);
        System.out.println(myEnum2);
        System.out.println(myEnum1 == myEnum2);

        // 通过反射获取枚举
        // 1.获取枚举的类类型
        Class<?> clazz = MyEnum.class;

        // 2.查看字节码文件，知道构造器之后，取消权限访问检查，创建构造器，并创建实例

        // 方式1：javap 准确度不高
        // 在out文件中，找到对应的MyEnum.class文件，右击并选择【Open in Terminal】
        // 使用javap -p MyEnum.class
        // 构造方法： private com.yeban.javase.day20.reflect.singleton.MyEnum();
        /*Constructor<?> constructor = clazz.getDeclaredConstructor();
        constructor.setAccessible(true);
        MyEnum myEnum = (MyEnum) constructor.newInstance();
        // 不匹配
        System.out.println("myEnum = " + myEnum);*/

        // 方式2：IDEA编译器:【View】 -> 【Show Bytecode】
        // 找到 private <init>(Ljava/lang/String;I)V
        /*Constructor<MyEnum> constructor1 = clazz.getDeclaredConstructor(String.class, int.class);
        constructor1.setAccessible(true);
        MyEnum myEnum = constructor1.newInstance();
        System.out.println("myEnum = " + myEnum);*/
    }
}
